package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.common.ConfigParam;

public class DataBase {

	private String url = "jdbc:mysql://{DBIP}/{DBNAME}";
	private String name = "com.mysql.jdbc.Driver";
	private String user ;
	private String password ;

	public ResultSet resultSet = null;
	public Connection conn = null;
	public PreparedStatement preparedStatement = null;

	public DataBase() {

		try {

			url = url.replace("{DBIP}", ConfigParam.get("IP"))
					.replace("{DBNAME}", ConfigParam.get("DBNAME"));

			user = ConfigParam.get("NAME");
			password = ConfigParam.get("PASSW");

			Class.forName(name);//指定连接类型
			conn = DriverManager.getConnection(url, user, password);//获取连接
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 *
	 */
	public void close(){
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != preparedStatement){
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != conn){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
